package com.example.demo.controller;

import com.example.demo.entity.SearchHistory;
import com.example.demo.service.SearchHistoryService;
import com.example.demo.service.impl.SearchHistoryServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/app/search-history")
public class SearchHistoryController {

    private final SearchHistoryService searchHistoryService;

    @Autowired
    public SearchHistoryController(SearchHistoryServiceImpl searchHistoryServiceImpl){
        this.searchHistoryService = searchHistoryServiceImpl;
    }

    // 获取当前用户的搜索历史
    @GetMapping("/get/history")
    public List<String> getHistory(@RequestParam Long userId) {
        System.out.println("调用了*List<SearchHistory> getHistory(@RequestParam Long userId)");
        return searchHistoryService.findByUserIdOrderBySearchTimeDesc(userId);
    }

    // 添加搜索历史
    @PostMapping("/post/add")
    public SearchHistory addHistory(@RequestBody SearchHistory history) {
        System.out.println("调用了&List<SearchHistory> getHistory(@RequestParam Long userId)");
        return searchHistoryService.save(history);
    }

    // 清空历史
    @DeleteMapping("/delete/byUserId")
    public void clearHistory(@RequestParam Long userId) {
        System.out.println("调用了@List<SearchHistory> getHistory(@RequestParam Long userId)");
        searchHistoryService.deleteByUserId(userId);
    }
}
